//
//  BaseTabBar.m
//  SleepingElf
//
//  Created by karong on 2023/6/5.
//

#import "BaseTabBar.h"
@interface BaseTabBar ()
@end

@implementation BaseTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.translucent = YES;
        self.backgroundColor = [UIColor colorFromHexRGB:@"212854"];
        [self setShadowImage:[[UIImage alloc]init]];
        [self setBackgroundImage:[[UIImage alloc]init]];
        _oldSafeAreaInsets = UIEdgeInsetsZero;
        self.clipsToBounds = NO;
        [self addSubview:self.slideImageView];
    }
    return self;
}

- (UIImageView *)slideImageView {
    if (!_slideImageView) {
        UIImage * image = SEIMG(@"icon_tab_slider");
        _slideImageView = [[UIImageView alloc]initWithImage:image];
        _slideImageView.frame = CGRectMake((SEScreenWidth / 4.0f - image.size.width) / 2.0f, -image.size.height, image.size.width, image.size.height);
    }
    return _slideImageView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        CGRect newFrame = [change[NSKeyValueChangeNewKey] CGRectValue];
        if (newFrame.size.height == 33) {
            newFrame.size.height = 48;
            ((UIView *)object).frame = newFrame;
        }
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    NSArray *items = self.items;
    NSInteger index = 0;
    for (UITabBarItem *item in items) {
        if (@available(iOS 13.0, *)) {
            self.tintColor = UIColor.font_blue_8BA8F2;
            [self setUnselectedItemTintColor:UIColor.font_gray_727584];
        } else {
            NSDictionary *normalAttrDict = @{NSForegroundColorAttributeName:UIColor.font_gray_727584, NSFontAttributeName: [UIFont systemFontOfSize:12]};
            NSDictionary *selectedAttrDict = @{NSForegroundColorAttributeName:UIColor.font_blue_8BA8F2, NSFontAttributeName: [UIFont systemFontOfSize:12]};
            [item setTitleTextAttributes:normalAttrDict forState:UIControlStateNormal];
            [item setTitleTextAttributes:selectedAttrDict forState:UIControlStateSelected];
        }
        
        
        item.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [item setTitlePositionAdjustment:UIOffsetMake(0, -2)];
        if (index == 0) {
            item.image = [SEIMG(@"icon_tab_home_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [SEIMG(@"icon_tab_home_s") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = WSLocalized(@"首页");
        }
        else if (index == 1) {
            item.image = [SEIMG(@"icon_tab_sleep_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [SEIMG(@"icon_tab_sleep_s") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = WSLocalized(@"睡眠");
        }
        else if (index == 2) {
            item.image = [SEIMG(@"icon_tab_music_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [SEIMG(@"icon_tab_music_s") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = WSLocalized(@"音乐");
        }
        else {
            item.image = [SEIMG(@"icon_tab_sleep_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [SEIMG(@"icon_tab_sleep_s") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = WSLocalized(@"我的");
        }
        index ++;
    }
}

@end
